package FCRendering.sw;

import FCRendering.FCRConstants;
import FCRendering.FCRMaster;
import Frame.sayoptional.SayOptionalHelper;
import Frame.screen.IRListener;
import Frame.shopSet.FunctionTalkToParnerShop;
import Function.FCMutilPlayCache;
import Function.FCMutilPlayPropulsionCache;
import Function.ItemUse.FCItemUseCenter;
import VWorldGameStudio.*;
import VWorldGameStudio.events.*;
import io.reactivex.schedulers.Schedulers;
import socket.FCRSocketClient;
import socket.FCRSocketServer;
import utils.ClickHelper;
import utils.FCMixUtil;

import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;

import static FCRendering.FCRMaster.*;

public class FCRListen {

    private HashMap<Integer, IRListener> listenerHashMap = new HashMap<>();

    private LinkedList<String> moveKeys = new LinkedList<>();
    private ClickHelper timeHelper = new ClickHelper(100);//表示100耗秒内接下一个行走

    /**
     * @param num      fcrvalue 的interfacenum
     * @param listener
     */
    public void addIRListener(int num, IRListener listener) {
        listenerHashMap.put(num, listener);
    }

    /**
     * 监听输入的玩家昵称
     *
     * 监听文本框中玩家名称的输入，并进行格式限制
     * 当玩家名称的长度超过10时，自动截取前10个字符
     * 当玩家名称中包含非字母和数字的字符时，自动移除该字符
     *
     * @param playerName
     */
    public void listen_key_release_playername(JTextField playerName) {
        if (playerName.getText().length() > 10)
            playerName.setText(playerName.getText().substring(0, 10));
        if (playerName.getText().length() > 0)
            for (int i = 0; i < playerName.getText().length(); i++) {
                char a = playerName.getText().charAt(i);
                if (!((a > 47 && a < 58) || (a > 64 && a < 91) || (a > 96 && a < 123))) {
                    playerName.setText(playerName.getText().replace(a + "", ""));
                    i--;
                }
            }
    }

    /**
     * 监听游戏按键松开
     *
     * @param e
     */
    public void listen_key_release_game(KeyEvent e) {
        String zxc = FCRMaster.hotkey.getCodeKeyOfKeyCode(e.getKeyCode());
        if (zxc != null) {
            System.out.println(System.currentTimeMillis() + " listen_key_release_game " + e.getKeyCode());
            if (zxc.equals("down")) {
                moveKeys.remove("down");
                FCRMaster.fcv.setDirDown(false);
                if (moveKeys.size() == 0) {
                    player1.setActionDir(0);
                    FCRMaster.fcv.setBindingPlayerName(null);
                    syncActionDir();
                    timeHelper.canClick();
                } else if (zxc.equals(nowMoveKey)) {//放开的是正在按的按钮
                    lastMoveKey = moveKeys.peekFirst();
                    moveKeyEventRun.run();
                }
            } else if (zxc.equals("up")) {
                moveKeys.remove("up");
                FCRMaster.fcv.setDirUp(false);
                if (moveKeys.size() == 0) {
                    player1.setActionDir(3);
                    FCRMaster.fcv.setBindingPlayerName(null);
                    syncActionDir();
                    timeHelper.canClick();
                } else if (zxc.equals(nowMoveKey)) {//放开的是正在按的按钮
                    lastMoveKey = moveKeys.peekFirst();
                    moveKeyEventRun.run();
                }
            } else if (zxc.equals("left")) {
                moveKeys.remove("left");
                FCRMaster.fcv.setDirLeft(false);
                if (moveKeys.size() == 0) {
                    player1.setActionDir(1);
                    FCRMaster.fcv.setBindingPlayerName(null);
                    syncActionDir();
                    timeHelper.canClick();
                } else if (zxc.equals(nowMoveKey)) {//放开的是正在按的按钮
                    lastMoveKey = moveKeys.peekFirst();
                    moveKeyEventRun.run();
                }
            } else if (zxc.equals("right")) {
                moveKeys.remove("right");
                FCRMaster.fcv.setDirRight(false);
                if (moveKeys.size() == 0) {
                    player1.setActionDir(2);
                    FCRMaster.fcv.setBindingPlayerName(null);
                    syncActionDir();
                    timeHelper.canClick();
                } else if (zxc.equals(nowMoveKey)) {//放开的是正在按的按钮
                    lastMoveKey = moveKeys.peekFirst();
                    moveKeyEventRun.run();
                }
            }
        }
    }

    private void syncActionDir() {
        // 同步玩家行动方向到服务器或客户端
        if (FCRMaster.fcv.isSever()) {
            FCRSocketServer.getIn().playerMoving();
        } else if (FCRMaster.fcv.isClient()) {
            FCRSocketClient.getIn().playerMoving();
        }
    }

    private String lastMoveKey = "", nowMoveKey = "";

    private MoveKeyEvent moveKeyEventRun = new MoveKeyEvent();

    public class MoveKeyEvent implements Runnable {

        @Override
        public void run() {
            // 如果没有移动键被按下，则直接返回
            if (moveKeys.size() == 0) {
                return;
            }
            // 如果当前界面不是游戏界面，则直接返回
            if (FCRMaster.fcv.getInterfacenum() != 5) {
                return;
            }
            // 如果当前处于战斗状态，则直接返回
            if (FCRMaster.fcv.isBattle()) {
                return;
            }
            // 如果某些UI界面打开，则不执行移动逻辑
            if (!FCRMaster.fcv.isShopVedingOpen() && !FCRMaster.fcv.isOpenCase() && !FCRMaster.fcv.isOpenSay() && !FCRMaster.fcv.isOpenPhonograph()
                    && !FCRMaster.fcv.isOpenUseTankMenu() && !fcv.isInTeamLink()) {
                nowMoveKey = lastMoveKey;
                System.out.println(System.currentTimeMillis() + " 切换到行走=" + nowMoveKey);
                // 长按移动键时，设置相应的方向
                if (lastMoveKey.equals("down")) {
                    FCRMaster.fcv.setBindingPlayerName(null);
                    FCRMaster.fcv.setDirDown(true);
                    FCRMaster.fcv.setDirUp(false);
                    FCRMaster.fcv.setDirLeft(false);
                    FCRMaster.fcv.setDirRight(false);
                } else if (lastMoveKey.equals("up")) {
                    FCRMaster.fcv.setBindingPlayerName(null);
                    FCRMaster.fcv.setDirDown(false);
                    FCRMaster.fcv.setDirUp(true);
                    FCRMaster.fcv.setDirLeft(false);
                    FCRMaster.fcv.setDirRight(false);
                } else if (lastMoveKey.equals("left")) {
                    FCRMaster.fcv.setBindingPlayerName(null);
                    FCRMaster.fcv.setDirDown(false);
                    FCRMaster.fcv.setDirUp(false);
                    FCRMaster.fcv.setDirLeft(true);
                    FCRMaster.fcv.setDirRight(false);
                } else if (lastMoveKey.equals("right")) {
                    FCRMaster.fcv.setBindingPlayerName(null);
                    FCRMaster.fcv.setDirDown(false);
                    FCRMaster.fcv.setDirUp(false);
                    FCRMaster.fcv.setDirLeft(false);
                    FCRMaster.fcv.setDirRight(true);
                }
            }
        }
    }

    /**
     * 监听游戏按键按下
     *
     * @param e
     *//**
     * 处理游戏中的键盘按下事件
     * @param e 键盘事件对象，包含按键信息
     */
    public void listen_key_press_game(KeyEvent e) {
        // 记录键盘事件发生的时间和按键代码
        System.out.println(System.currentTimeMillis() + "  listen_key_press_game " + e.getKeyCode());

        // 检查当前界面编号是否为5，即特定的游戏界面
        if (FCRMaster.fcv.getInterfacenum() == 5) {
            // 获取按键对应的热键字符串
            String str = FCRMaster.hotkey.getCodeKeyOfKeyCode(e.getKeyCode());

            // 如果按键对应热键字符串不为空
            if (str != null) {
                // 处理帮助UI的显示和隐藏
                if (str.equals("I")) {
                    fcv.setShowHelpUI(!fcv.isShowHelpUI());
                    return;
                }

                // 如果处于战斗状态，委托给相应的监听器处理按键事件
                if (FCRMaster.fcv.isBattle()) {
                    listenerHashMap.get(6).listen_key_press_game(str);
                    return;
                }

                // 处理回车键事件，控制消息文本框的显示和隐藏
                if (str.equals("enter")) {
                    if (FCRMaster.messageText.isVisible()) {
                        FCRMaster.messageText.setVisible(false);
                        player1.setTalkingMsg(null);
                    } else {
                        FCRMaster.messageText.setText("");
                        FCRMaster.messageText.setVisible(true);
                        FCRMaster.messageText.requestFocus();
                    }
                    return;
                }

                // 处理数字键事件，用于选择背包中的物品
                if (e.getKeyCode() >= 48 && e.getKeyCode() <= 58) {
                    FCPlayerInfo fcPlayerInfo = FCMutilPlayCache.findPlayerByName(gameSave.HOST_UID, fcv.getShowWhosBagItem());
                    int usIndex = e.getKeyCode() - 48;
                    if (usIndex == 0) {
                        usIndex = 9;
                    } else {
                        usIndex -= 1;
                    }
                    FCItem sItem = itemLib.getItemOfID(fcPlayerInfo.getKnapsack()[usIndex]);
                    if (sItem != null) {
                        FCItemUseCenter.getIn().findItemUseMethod(fcPlayerInfo, null, sItem.getName(), usIndex, false)
                                .directUse();
                    }
                    return;
                }

                // 如果没有打开商店、箱子、对话等界面
                if (!FCRMaster.fcv.isShopVedingOpen() && !FCRMaster.fcv.isOpenCase() && !FCRMaster.fcv.isOpenSay() && !FCRMaster.fcv.isOpenPhonograph()
                        && !FCRMaster.fcv.isOpenUseTankMenu() && !fcv.isInTeamLink()) {
                    // 处理方向键事件，用于控制角色移动
                    if (str.equals("down") || str.equals("up") || str.equals("left") || str.equals("right")) {
                        lastMoveKey = str;
                        if (!moveKeys.contains(str)) {
                            boolean clicklong = timeHelper.canClick();
                            if (clicklong && moveKeys.size() == 0) {
                                moveKeys.addFirst(str);
                                Schedulers.io().scheduleDirect(moveKeyEventRun, 80, TimeUnit.MILLISECONDS);
                            } else {
                                moveKeys.addFirst(str);
                                moveKeyEventRun.run();
                            }
                        } else {
                            if (!moveKeys.peekFirst().equals(str)) {
                                moveKeys.remove(str);
                                moveKeys.addFirst(str);
                            }
                        }
                    }
                }

                // 处理对话和调查功能
                if (str.equals("say")) {
                    FCRMaster.sound.setEffectSound(14);
                    if (FCRMaster.fcv.isOpenSay()) {
                        clickSay();
                    } else {
                        talkAndCheck();
                    }
                } else if (str.equals("menu")) {
                    FCRMaster.sound.setEffectSound(14);
                    // 处理菜单相关操作
                    if (FCRMaster.fcv.isOpenUseItem()) {
                        FCRMaster.fcv.setOpenUseItem(false);
                        return;
                    }
                    if (FCRMaster.fcv.isOpenMMWVIP()) {
                        FCRMaster.fcv.setOpenMMWVIP(false);
                        return;
                    }
                    if (FCRMaster.fcv.isOpenTanCheResult()) {
                        FCRMaster.fcv.setOpenTanCheResult(false);
                        return;
                    }
                    if (FCRMaster.fcv.getShowTankChuanzhenNames().size() > 0) {
                        FCRMaster.fcv.getShowTankChuanzhenNames().clear();
                        return;
                    }
                    if (FCRMaster.fcv.isShopVedingOpen() || FCRMaster.fcv.isOpenCase() || FCRMaster.fcv.isOpenSay() || FCRMaster.fcv.isOpenPhonograph() || FCRMaster.fcv.isOpenUseTankMenu()) {
                        if (FCRMaster.fcv.isOpenCase()) {
                            FCRMaster.gameSave.setCaseUpdateInfo(FCRMaster.fcv.getPlayingEvent().getName(), ((CaseEvent) FCRMaster.fcv.getPlayingEvent()).getCaseitem());
                        }
                        if (FCRMaster.fcv.getShopVendingTip().indexOf(FCRConstants.SVT_LOTTERY) < 0) {
                            FCRMaster.fcv.setShopVedingOpen(false);
                        }
                        if (FCRMaster.fcv.getShopVendingSelectNum() == 0) {
                            FCRMaster.fcv.setOpenCase(false);
                        }
                        FCRMaster.fcv.setOpenSay(false);
                        FCRMaster.fcv.setOpenSayShop(false);
                        FCRMaster.fcv.setOpenPhonograph(false);
                        FCRMaster.fcv.setOpenUseTankMenu(false);
                        FCRMaster.fcv.setSayNpcName(null);
                        FCRMaster.fcv.setSayNpcTextNum(0);
                    } else {
                        FCRMaster.fcv.getLookIntos().clear();
                        FCRMaster.fcv.getShowChuanzhenNames().clear();
                        FCRMaster.fcv.getShowTankChuanzhenNames().clear();
                        FCRMaster.fcv.setOpenMenu(!FCRMaster.fcv.isOpenMenu());
                        if (FCRMaster.fcv.isOpenMenu()) {
                            FCMixUtil.resetTankNameList();
                        }
                    }
                } else if (str.equals("Q")) {
                    if (FCRMaster.fcv.isOpenMenu() && FCRMaster.fcv.getOpenMenuNum() == 1) {
                        if (FCRMaster.fcv.getTanknameList().size() > 0) {
                            int willIndex = FCRMaster.fcv.getShowTankMenuTankIndex() + 1;
                            if (willIndex >= FCRMaster.fcv.getTanknameList().size()) {
                                willIndex = 0;
                            }
                            FCRMaster.fcv.setShowingTankMenuDetail(null);
                            FCRMaster.fcv.setShowTankMenuTankIndex(willIndex);
                        }
                    } else {
                        FCMixUtil.changeShowPlayerBagByKey();
                    }
                } else if (str.equals("X")) {
                    if (gameSave.isHasStartMapFunction()) {
                        fcv.setShowMapImg(!fcv.isShowMapImg());
                    }
                }
            }
        }
    }


    /**
     * 执行点击说话按钮的逻辑
     * 该方法处理玩家与NPC或物品的对话流程，包括对话文本的更新和对话事件的管理
     */
    public void clickSay() {
        // 清空当前的对话文本
        FCRMaster.fcv.setSayText(null);

        // 清空对话中的NPC选择菜单
        FCRMaster.fcv.getSayNpcSelectMenu().clear();

        // 检查当前是否有NPC对话名称，以决定是否继续对话流程
        if (FCRMaster.fcv.getSayNpcName() == null) {
            // 如果没有指定NPC名称，关闭对话窗口和商店对话状态
            FCRMaster.fcv.setOpenSay(false);
            FCRMaster.fcv.setOpenSayShop(false);
            FCRMaster.fcv.setSayNpcTextNum(0);
            FCRMaster.fcv.setBattleEvent(null);
        } else {
            // 如果有指定NPC名称，更新对话文本编号并获取相应的对话事件
            FCRMaster.fcv.setSayNpcTextNum(FCRMaster.fcv.getSayNpcTextNum() + 1);
            FCEvent nEvent = FCRMaster.eventLib.getEventOfName(FCRMaster.charLib.getCharacterOfName(FCRMaster.fcv.getSayNpcName()).getCharacterEventID());
            SayEvent sayEvent = null;

            // 根据事件类型转换目标事件
            if (nEvent instanceof BattleEvent) {
                sayEvent = (SayEvent) FCRMaster.eventLib.getEventOfName(((BattleEvent) nEvent).getContainSayTextId());
            } else if (nEvent instanceof SayEvent) {
                sayEvent = (SayEvent) nEvent;
            } else {
                // 如果事件类型不正确，输出错误信息并关闭对话窗口
                System.out.println("-事件类型出错-" + nEvent.getType());
                FCRMaster.fcv.setOpenSay(false);
                FCRMaster.fcv.setOpenSayShop(false);
                FCRMaster.fcv.setSayNpcTextNum(0);
                FCRMaster.fcv.setBattleEvent(null);
                return;
            }

            // 检查当前对话文本编号是否超过对话事件的文本数量
            if (FCRMaster.fcv.getSayNpcTextNum() >= fcv.getSaytext(sayEvent).size()) {
                // 如果超过，关闭对话窗口并重置相关状态
                FCRMaster.fcv.setOpenSay(false);
                FCRMaster.fcv.setOpenSayShop(false);
                FCRMaster.fcv.setSayNpcName(null);
                FCRMaster.fcv.setBattleEvent(null);
                FCRMaster.fcv.setSayNpcTextNum(0);
                FCRMaster.fcv.setLastEndSayEventId(sayEvent.getName());

                // 如果对话事件包含多个文本选项，更新对话事件的当前选项
                if (sayEvent.getSayTextCount() > 1) {
                    Integer curSelectNum = gameSave.getSayEventTalkSelects().get(sayEvent.getName());
                    if (curSelectNum == null) {
                        curSelectNum = 0;
                    }
                    FCRMaster.fcv.setLastEndSayNum(curSelectNum);
                    if (curSelectNum == sayEvent.getSayTextCount() - 1) {
                        curSelectNum = 0;
                    } else {
                        curSelectNum++;
                    }
                    System.out.println("切换到下一个对话内容 " + curSelectNum);
                    gameSave.getSayEventTalkSelects().put(sayEvent.getName(), curSelectNum);
                }
            } else {
                // 如果未超过文本数量，更新对话文本并创建对话菜单
                FCRMaster.fcv.setSayText(fcv.getSayTextOfNum(FCRMaster.fcv.getSayNpcTextNum(), sayEvent).split("<option"));
                SayOptionalHelper.getIn().createSayMenu();
            }
        }
    }


    //调查和对话
    private void talkAndCheck() {
        if (FCRMaster.fcv.isOpenUseTankMenu()) {
            FCRMaster.fcv.setOpenUseTankMenu(false);
            return;
        }
        if (FCRMaster.fcv.isOpenCase()) {
            FCRMaster.fcv.setOpenCase(false);
            return;
        }
        if (FCRMaster.fcv.isShopVedingOpen()) {
            FCRMaster.fcv.setShopVedingOpen(false);
            return;
        }
        if (FCRMaster.fcv.isOpenPhonograph()) {
            FCRMaster.fcv.setOpenPhonograph(false);
            return;
        }
        if (FCRMaster.fcv.isOpenUseItem()) {
            FCRMaster.fcv.setOpenUseItem(false);
            return;
        }

        if (FCRMaster.fcv.isOpenMMWVIP()) {
            FCRMaster.fcv.setOpenMMWVIP(false);
            return;
        }
        if (FCRMaster.fcv.isOpenTanCheResult()) {
            FCRMaster.fcv.setOpenTanCheResult(false);
            return;
        }
        if (FCRMaster.fcv.getShowChuanzhenNames().size() > 0) {
            FCRMaster.fcv.getShowChuanzhenNames().clear();
            return;
        }
        if (FCRMaster.fcv.getShowTankChuanzhenNames().size() > 0) {
            FCRMaster.fcv.getShowTankChuanzhenNames().clear();
            return;
        }

        if (fcv.getLookIntos().size() > 0) {
            int showLen = Math.min(5, fcv.getLookIntos().size());
            for (int i = 0; i < showLen; i++) {
                fcv.getLookIntos().pollLast();
            }
            return;
        }
        if (!FCRMaster.fcv.isShopVedingOpen() && !FCRMaster.fcv.isOpenCase() && !FCRMaster.fcv.isOpenSay() && !FCRMaster.fcv.isOpenPhonograph()) {
            int dir = FCRMaster.player1.getActionDir();// up3,down0,left1,right2
            int dx = 0, dy = 0;
            if (dir == 0)
                dy = 1;
            if (dir == 3)
                dy = -1;
            if (dir == 1)
                dx = -1;
            if (dir == 2)
                dx = 1;

            FCRMaster.fcv.setPlayingEvent(FCRMaster.eventLib.getEventOfName(
                    FCRMaster.mapLib.getMapOfName(FCRMaster.player1.getMapname()).getEventNameOfCoor(
                            FCRMaster.player1.getPlayer1CoorX() + dy, FCRMaster.player1.getPlayer1CoorY() + dx)));
            int oneStepX = dx;
            int oneStepY = dy;
            //   if (!isOtherEvent) {// 与人对话
            boolean hasTalk = false;
            FCRMaster.fcv.getSayNpcSelectMenu().clear();
            String asd = (FCRMaster.player1.getPlayer1CoorX() + dy) + "*" + (FCRMaster.player1.getPlayer1CoorY() + dx);
            String aNpcName = FCRMaster.fcv.checkAndGetNpcInXYTag(asd);
            if (aNpcName != null) {// 前方一格处有人
                talkToHim(aNpcName, asd, 1);
                hasTalk = true;
            } else {
                String blockname = mapLib.getMapOfName(player1.getMapname())
                        .getPlaceNameOfCoor(FCRMaster.player1.getPlayer1CoorX() + dy, (FCRMaster.player1.getPlayer1CoorY() + dx));
                if (dir == 0)
                    dy += 1;
                if (dir == 3)
                    dy += -1;
                if (dir == 1)
                    dx += -1;
                if (dir == 2)
                    dx += 1;
                asd = (FCRMaster.player1.getPlayer1CoorX() + dy) + "*" + (FCRMaster.player1.getPlayer1CoorY() + dx);
                String bNpcName = FCRMaster.fcv.checkAndGetNpcInXYTag(asd);
                if (bNpcName != null) {// 前方两格远处有人
                    talkToHim(bNpcName, asd, 2);
                    hasTalk = true;
                } else {
                    if (blockname.startsWith("Men")) {//判断如果前面是门能执行第三格的
                        if (dir == 0)
                            dy += 1;
                        if (dir == 3)
                            dy += -1;
                        if (dir == 1)
                            dx += -1;
                        if (dir == 2)
                            dx += 1;
                        asd = (FCRMaster.player1.getPlayer1CoorX() + dy) + "*" + (FCRMaster.player1.getPlayer1CoorY() + dx);
                        String cNpcName = FCRMaster.fcv.checkAndGetNpcInXYTag(asd);
                        if (cNpcName != null) {//前方三格有人
                            talkToHim(cNpcName, asd, 3);
                            hasTalk = true;
                        }
                    }
                }
            }
            // }
            if (!hasTalk) {
                boolean isOtherEvent = lookIntoEventDeal(FCRMaster.player1.getPlayer1CoorX() + oneStepY, FCRMaster.player1.getPlayer1CoorY() + oneStepX);
                if (!isOtherEvent) {
                    fcv.getLookIntos().add("什么也没有！");
                }
            }
        }
    }

    private void talkToHim(String aNpcName, String asd, int distance) {
        FCRMaster.fcv.setSayNpcName(aNpcName);
        FCCharacter findCharacter = FCRMaster.charLib.getCharacterOfName(FCRMaster.fcv.getSayNpcName());
        FCEvent qw = FCRMaster.eventLib.getEventOfName(findCharacter.getCharacterEventID());
        if (qw != null) {
            String key = FCRMaster.player1.getMapname() + "*" + asd;
            if (qw.getType().equals("battle") && distance == 1) {
                BattleEvent battleEvent = (BattleEvent) qw;
                if (!gameSave.getBattleBossMaps().containsKey(key)) {
                    fcv.setBattleBossRecord(new FCBossRecord(findCharacter.getCharacterName(), FCRMaster.player1.getMapname(), asd));
                    SayEvent sayEvent = (SayEvent) FCRMaster.eventLib.getEventOfName(battleEvent.getContainSayTextId());
                    fcv.setBattleEvent(qw);
                    if (sayEvent != null) {
                        //有对话咯
                        FCRMaster.fcv.setSayText(fcv.getSayTextOfNum(0, sayEvent).split("<option"));
                        SayOptionalHelper.getIn().createSayMenu();
                        FCRMaster.fcv.setOpenSay(true);
                    } else {
                        //TODO 没对话的就直接打了
                        //TODO 剧情没过，打吧
                        fcv.setBattleRounds(1);
                        fcv.setMonsterBuilding(true);
                        fcv.setOldMusicNum(sound.getNowMusicNum());
                        sound.setBackgroundMusic(8);
                    }
                }
            } else if (!gameSave.getOtherEventMaps().containsKey(key)) {
                //既然对话的是人，那就肯定是对话了
                SayEvent sayEvent = (SayEvent) qw;
                FCRMaster.fcv.setOtherEventKey(key);
                FCRMaster.fcv.setSayText(fcv.getSayTextOfNum(0, sayEvent).split("<option"));
                SayOptionalHelper.getIn().createSayMenu();
                FCRMaster.fcv.setOpenSay(true);
            }
        }
    }

    private boolean lookIntoEventDeal(int x, int y) {

        if (FCRMaster.fcv.getPlayingEvent() != null) {
            String type = FCRMaster.fcv.getPlayingEvent().getType();
            if (type.equals("shop")) {// 售货机,调查功能的一种
                FCRMaster.fcv.setShopVedingOpen(true);
                FCRMaster.fcv.setShopLampNum(-1);
                return true;
            } else if (type.equals("case")) {// 箱子调查功能的一种
                FCRMaster.fcv.setOpenCase(true);
                FCRMaster.sound.setEffectSound(20);
                long mm = ((CaseEvent) FCRMaster.fcv.getPlayingEvent()).getCaseMoney();
                if (mm > 0) {
                    FCRMaster.player1.addMoney(mm);
                    FCRMaster.fcv.getMessage().add(FCRConstants.SYSTEM + FCRConstants.SEARCH + FCRConstants.OPEN_BOX + FCRConstants.GET + mm + FCRConstants.COINS);
                    FCRMaster.fcv.setMessageShowNum(300);
                    ((CaseEvent) FCRMaster.fcv.getPlayingEvent()).setCaseMoney();
                    FCRMaster.gameSave.setCaseUpdateInfo(FCRMaster.fcv.getPlayingEvent().getName(), ((CaseEvent) FCRMaster.fcv.getPlayingEvent()).getCaseitem());
                }
                return true;
            } else if (type.equals("phonograph")) {// 留声机调查功能的一种
                PhonographEvent phonographEvent = (PhonographEvent) FCRMaster.fcv.getPlayingEvent();
                FCRMaster.fcv.setPhonographMusicName(phonographEvent.getPhonographname());
                FCRMaster.fcv.setPhonographMusicNum(phonographEvent.getPhonographnum());
                FCRMaster.fcv.setOpenPhonograph(true);
                return true;
            } else if (type.equals("itemCtrl")) {// 物品对话，选项W
                String key = FCRMaster.player1.getMapname() + "*" + x + "*" + y;
                if (!gameSave.getOtherEventMaps().containsKey(key)) {
                    FCEvent sayEvent = eventLib.getEventOfName(((ItemCtrlEvent) FCRMaster.fcv.getPlayingEvent()).getContainSayTextId());
                    if (sayEvent != null && sayEvent.getType().equals("say")) {
                        FCRMaster.fcv.setSayNpcName(null);
                        FCRMaster.fcv.setSayText(fcv.getSayTextOfNum(0, (SayEvent) sayEvent).split("<option"));
                        SayOptionalHelper.getIn().createSayMenu();
                        FCRMaster.fcv.setOpenSay(true);
                        return true;
                    }
                }
            } else if (type.equals("openChuanzhen")) {
                //打开传真界面就行了
                Iterator<Map.Entry<String, String>> iterator = gameSave.getChuanZhenMaps().entrySet().iterator();
                while (iterator.hasNext()) {
                    fcv.getShowChuanzhenNames().add(iterator.next().getKey());
                }
                return true;
            } else if (type.equals("door")) {
                if (!gameSave.getSaveAllHasUnLockDoors().contains(FCRMaster.fcv.getPlayingEvent().getName())) {
                    //TODO 这个门还没开过的
                    FCItem key = itemLib.getItemOfID(((DoorEvent) FCRMaster.fcv.getPlayingEvent()).getNeedItemKey());
                    fcv.getLookIntos().add("门: 需要" + key.getChName() + "打开");
                } else {
                    fcv.getLookIntos().add("没锁的门");
                }
                return true;
            }
        } else {
            HashMap<String, String> maptank = FCRMaster.mapLib.getMapOfName(FCRMaster.player1.getMapname()).getMapTankHashMap();
            //发现一切除了地图外的东西就行了,可以调查物品，坦克，别的玩家人物，
            fcv.getLookIntos().clear();

            //判断获取地图原生未获取的坦克
            for (String coor : maptank.keySet()) {
                //coor = x*y  maptank.get(coor) 是坦克id
                FCTank find = gameSave.findTankSaveIos(maptank.get(coor));
                if (find == null) {
                    if (coor.equals(x + "*" + y)) {
                        String endTag = "(归属你的世界)";
                        fcv.getLookIntos().add(FCRMaster.langLib.getNameOfId(maptank.get(coor)) + endTag);
                    }
                }
            }

            //还有非队伍里的其他玩家
            Iterator<Map.Entry<String, FCPlayerInfo>> iterator = FCMutilPlayCache.gamePlayers.entrySet().iterator();
            while (iterator.hasNext()) {
                FCPlayerInfo p = iterator.next().getValue();
                if (p.getMapname().equals(player1.getMapname())) {
                    if (x == p.getPlayer1x() && y == p.getPlayer1y()) {
                        String endTag = "(归属你的世界)";
                        if (!p.getAliasUID().equals(gameSave.HOST_UID)) {
                            endTag = "(归属别的世界)";
                        } else {
                            if (p.isANpcPlayer()) {
                                //只能是自己自家人了
                                fcv.setCurrentAShopFuntion(new FunctionTalkToParnerShop(p));
                                fcv.setOpenSayShop(true);
                                fcv.setOpenSay(true);
                                return true;
                            }
                        }
                        if (p.getPlayertank1() != null && !p.getPlayertank1().equals("null")) {
                            fcv.getLookIntos().add(FCRMaster.langLib.getNameOfId(p.getPlayertank1()) + endTag + (p.getTalkingMsg() != null ? ":" + p.getTalkingMsg() : ""));
                        } else {
                            fcv.getLookIntos().add(p.getChName() + endTag + (p.getTalkingMsg() != null ? ":" + p.getTalkingMsg() : ""));
                        }
                    }
                }
            }


            CopyOnWriteArrayList<FCPropulsion> fcPropulsions = FCMutilPlayPropulsionCache.mapToPropulsionMap.get(player1.getMapname());
            if (fcPropulsions != null) {
                for (FCPropulsion propulsion : fcPropulsions) {
                    if (propulsion.getVisable() == 1 && propulsion.getPcoorY() == y && propulsion.getPcoorX() == x) {
                        String endTag = "(归属你的世界)";
                        if (!propulsion.getPropulsionHostId().equals(gameSave.HOST_UID)) {
                            endTag = "(归属别的世界)";
                        }
                        fcv.getLookIntos().add(FCRMaster.langLib.getNameOfId(propulsion.getPropulsionID()) + endTag);
                    }
                }
            }
            if (fcv.getLookIntos().size() == 0) {
                String blockname = mapLib.getMapOfName(player1.getMapname()).getPlaceNameOfCoor(x, y);
                if (blockname.startsWith("ZhuoZi")) {
                    fcv.getLookIntos().add("桌子");
                } else if (blockname.startsWith("ZaoTai")) {
                    fcv.getLookIntos().add("灶台");
                } else if (blockname.startsWith("TieLu")) {
                    fcv.getLookIntos().add("铁路");
                } else if (blockname.startsWith("ShuMu")) {
                    fcv.getLookIntos().add("树木");
                } else if (blockname.startsWith("shuitong")) {
                    fcv.getLookIntos().add("水桶");
                } else if (blockname.startsWith("Shui")) {
                    fcv.getLookIntos().add("水");
                } else if (blockname.startsWith("Shan")) {
                    fcv.getLookIntos().add("山坡");
                } else if (blockname.startsWith("ShaDi")) {
                    fcv.getLookIntos().add("沙地");
                } else if (blockname.startsWith("QingCao")) {
                    fcv.getLookIntos().add("青草");
                } else if (blockname.startsWith("Qiao")) {
                    fcv.getLookIntos().add("桥");
                } else if (blockname.startsWith("QiangBi")) {
                    fcv.getLookIntos().add("墙壁");
                } else if (blockname.startsWith("LouTi")) {
                    fcv.getLookIntos().add("楼梯");
                } else if (blockname.startsWith("HuaPen")) {
                    fcv.getLookIntos().add("花盆");
                } else if (blockname.startsWith("GuiZi")) {
                    fcv.getLookIntos().add("柜子");
                } else if (blockname.startsWith("GongZuoTai")) {
                    fcv.getLookIntos().add("工作台");
                } else if (blockname.startsWith("chuansongdai")) {
                    fcv.getLookIntos().add("传送带");
                } else if (blockname.startsWith("Chuang")) {
                    fcv.getLookIntos().add("床");
                } else if (blockname.startsWith("Men")) {
                    fcv.getLookIntos().add("没锁的门");
                }
            }
            if (fcv.getLookIntos().size() > 0) {
                return true;
            }
        }
        return false;
    }


    /**
     * 监听鼠标移动
     *
     * @param g
     */
    public void listen_mouse_move_game(MouseEvent g) {
        FCRMaster.fcv.setMouseX(g.getX());
        FCRMaster.fcv.setMouseY(g.getY());
        int interfaceNum = FCRMaster.fcv.getInterfacenum();
        if (interfaceNum == 5 && FCRMaster.fcv.isBattle()) {
            interfaceNum = 6;
        }
        IRListener listener = listenerHashMap.get(interfaceNum);
        if (listener != null) {
            listener.listen_mouse_move_game(g);
        }
    }

    /**
     * 监听鼠标松开
     *
     * @param g
     */
    public void listen_mouse_release_game(MouseEvent g) {
        int interfaceNum = FCRMaster.fcv.getInterfacenum();
        if (interfaceNum == 5 && FCRMaster.fcv.isBattle()) {
            interfaceNum = 6;
        }
        IRListener listener = listenerHashMap.get(interfaceNum);
        if (listener != null) {
            listener.listen_mouse_release_game(g);
        }
    }

    /**
     * 监听鼠标按下
     *
     * @param g
     */
    public void listen_mouse_press_game(MouseEvent g) {

        int interfaceNum = FCRMaster.fcv.getInterfacenum();
        if (interfaceNum == 5 && FCRMaster.fcv.isBattle()) {
            interfaceNum = 6;
        }
        IRListener listener = listenerHashMap.get(interfaceNum);
        if (listener != null) {
            listener.listen_mouse_press_game(g);
        }
    }

    public void listen_mouse_click_game(MouseEvent g) {

        FCRMaster.sound.setEffectSound(14);
        int interfaceNum = FCRMaster.fcv.getInterfacenum();
        if (interfaceNum == 5 && FCRMaster.fcv.isBattle()) {
            interfaceNum = 6;
        }
        IRListener listener = listenerHashMap.get(interfaceNum);
        if (listener != null) {
            try {
                listener.listen_mouse_click_game(g);
            } catch (Exception e) {
            }
        }
    }

}
